package com.family.system.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.family.common.result.Result;
import com.family.model.system.SysRole;
import com.family.model.vo.SysRoleQueryVo;
import com.family.system.service.SysRoleService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Set;

/**
 * @ClassName SysRoleController
 * @Date 2023-05-05 13:43
 * @AUTHOR family
 **/
@Api(tags = "角色管理")
@RestController
@RequestMapping("/admin/system/sysRole")
public class SysRoleController {

    @Autowired
    private SysRoleService sysRoleService;
    @Autowired
    private RedisTemplate redisTemplate;


    @ApiOperation("查询所有角色信息")
    @GetMapping("findAll")
//    @PreAuthorize("hasAuthority('bnt.sysRole.list')")
    public Result<List<SysRole>> findAll() {
        List<SysRole> list = sysRoleService.list();
        return Result.ok(list);
    }

    @ApiOperation("获取分页列表")
    @GetMapping("findQueryPage/{page}/{limit}")
    @PreAuthorize("hasAuthority('bnt.sysRole.list')")
    public Result<Object> findByPage(
            @ApiParam(name = "page", value = "当前页码", required = true)
            @PathVariable Long page,
            @ApiParam(name = "limit", value = "每页条数", required = true)
            @PathVariable Long limit,
            @ApiParam(name = "roleQueryVo", value = "查询对象", required = true)
                    SysRoleQueryVo sysRoleQueryVo) {

        Object roleList = redisTemplate.opsForValue().get("roleList" + page);
        if (roleList != null) {
            return Result.ok(roleList);
        }
        IPage<SysRole> roleIPage = sysRoleService.selectPage(new Page<SysRole>(page, limit), sysRoleQueryVo);
        return Result.ok(roleIPage);
    }

    @ApiOperation("根据id删除")
    @DeleteMapping("remove/{id}")
    @PreAuthorize("hasAuthority('bnt.sysRole.remove')")
    public Result<Object> removeById(@PathVariable Long id) {
        boolean row = sysRoleService.removeById(id);
        if (!row) {
            return Result.fail();
        }
        Set<Object> keys = redisTemplate.keys("roleList" + "*");
        redisTemplate.delete(keys);
        return Result.ok();
    }

    @ApiOperation("添加角色信息")
    @PostMapping("save")
    @PreAuthorize("hasAuthority('bnt.sysRole.add')")
    public Result<Object> add(@RequestBody SysRole sysRole) {
        boolean row = sysRoleService.save(sysRole);
        if (!row) {
            return Result.fail();
        }
        Set<Object> keys = redisTemplate.keys("roleList" + "*");
        redisTemplate.delete(keys);
        redisTemplate.delete("allRoles");
        return Result.ok();
    }

    @ApiOperation("根据id查询数据回显")
    @GetMapping("get/{id}")
    @PreAuthorize("hasAuthority('bnt.sysRole.list')")
    public Result<SysRole> findById(@PathVariable Long id) {
        SysRole sysrole = sysRoleService.getById(id);
        return Result.ok(sysrole);
    }

    @ApiOperation("更新角色信息")
    @PutMapping("update")
    @PreAuthorize("hasAuthority('bnt.sysRole.update')")
    public Result<Object> update(@RequestBody SysRole sysRole) {
        boolean row = sysRoleService.updateById(sysRole);
        if (!row) {
            return Result.fail();
        }
        Set<Object> keys = redisTemplate.keys("roleList" + "*");
        redisTemplate.delete(keys);
        return Result.ok();
    }

    @ApiOperation("批量删除")
    @DeleteMapping("batchRemove")
    @PreAuthorize("hasAuthority('bnt.sysRole.remove')")
    public Result<Object> batchRemove(@RequestBody List<Long> idList) {
        boolean row = sysRoleService.removeByIds(idList);
        if (!row) {
            return Result.fail();
        }
        Set<Object> keys = redisTemplate.keys("roleList" + "*");
        redisTemplate.delete(keys);
        return Result.ok();
    }

}
